
function shakem(n) {
   if (self.moveBy) {
	for (i = 30; i > 0; i--) {
	   for (j = n; j > 0; j--) {
		self.moveBy(0,i);
		self.moveBy(i,0);
		self.moveBy(0,-i);
		self.moveBy(-i,0);
           }
       }
   }
}

function init()
{
	var Duration	= getCookie ('Duration') * 1;
	DisplayFormat (Duration);
	var cd_on=getCookie('cd_on');
	if (cd_on==1)
	{
		document.getElementById('startButton').innerHTML="";
		countdown();
	}
}

function cdStart()
{
		var startClock  = DateToMilSec(new Date());
		var endTime		= getCookie ('Duration') * 1;
		var endClock	= startClock + endTime;
		
		setCookie('cd_on',1, 365);
		
		setCookie("endClock",endClock,365);
		setCookie("zero",0,365);
		

		document.getElementById('startButton').innerHTML="";
		countdown();
	
}

function countdown( )
{	
		//ambil dari cookie
		var nowClock	= DateToMilSec (new Date ());
		var endClock	= getCookie ('endClock');
		var zero 		= getCookie ('zero');
	
		var timeLeft 	= endClock - nowClock;
		
		if (zero == 1) {
			timeLeft = 0;
			DisplayFormat ( timeLeft );
			$( "#cddiv" ).toggleClass( "colorred");
			$( "#cddiv" ).toggleClass( "colorwhite");
			t=setTimeout('countdown()',1000);
		}
		else if ((timeLeft < 1)&&(zero == 0)) {
			timeLeft = 0;
			DisplayFormat ( timeLeft );
			setCookie("zero",1,365);	
			shakem(120);
		}
		else if (timeLeft < 60000) {
			shakem(1);
		}
		if (zero == 0) {
			DisplayFormat ( timeLeft );
			t=setTimeout('countdown()',7);
		}
		
}

	
function DateToMilSec(d)
	{
		var h=d.getHours();
		var m=d.getMinutes();
		var s=d.getSeconds();
		var i=d.getMilliseconds();
		var x= h*3600000 + m*60000 + s*1000 + i;
		return x;
	}

function DisplayFormat(x)
	{
		var hour = div ( x , 3600000);
		var min  = div ( (x % 3600000), 60000);
		var sec  = div ( (x % 60000) , 1000);
		var mil  = x % 1000;

		mil=digit3(mil); min=digit2(min); sec=digit2(sec);hour=digit2(hour);

		//diedit disini
		var td = "<span class='ddot'>:</span>";
		var display = hour+td+min+td+sec+mil;

		document.getElementById('cddiv').innerHTML=display;
	
		return 1;
	}
	
function digit3(i)
{
	var nol = "<span class='milisec'>0</span>";
	if (i<100 && i>10) 
	  {
	  i= nol + "<span class='milisec'>"+ (div (i,10))+"</span>" + "<span class='milisec'>"+ (i%10)+"</span>";
	  }
	else if (i<10) 
	  {
	  i=nol + nol + "<span class='milisec'>"+i+"</span>";
	  }
	else if (i<1) {i= nol + nol +nol;}
	else {
	  i= "<span class='milisec'>"+ (div (i,100))+"</span>" + "<span class='milisec'>"+ (div ((i%100),10)) +"</span>" + "<span class='milisec'>"+ (i%10)+"</span>";
	  }
	return i;
}

function digit2(i)
{
	var nol = "<span class='cddigit'>0</span>";
	if (i<100 && i>9) 
	  {
	  i= "<span class='cddigit'>"+ (div (i,10))+"</span>" + "<span class='cddigit'>"+ (i%10)+"</span>";
	  }
	else if (i<10) 
	  {
	  i= nol + "<span class='cddigit'>"+i+"</span>";
	  }
	else if (i<1) {i= nol +nol;}
	return i;
}

function div (x,y)
{	
	return ((x - (x%y) )/ y);
}

function getCookie(c_name)
{
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
		{ 
		c_start=c_start + c_name.length+1; 
		c_end=document.cookie.indexOf(";",c_start);
		if (c_end==-1) c_end=document.cookie.length;
		return unescape(document.cookie.substring(c_start,c_end));
		} 
	  }
	return "";
}

function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

